package com.wolfalpha.utron.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.umeng.analytics.MobclickAgent;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.ui.UIHelper;

/**
 * 引导页
 *
 * @author luodon
 */
public class GuideActivity extends Activity {

    //viewFlipper
    private ViewFlipper vp;
    //指示器
    private ImageView ivInd1;
    private ImageView ivInd2;
    private ImageView ivInd3;
    private ImageView ivInd4;

    private Button btnEnter;
    GestureDetector gesture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        initView();
    }

    private void selectIndex(int i) {
        if (i == 1) {
            //第一页
            ivInd1.setImageResource(R.mipmap.guide_dot_selected);
            ivInd2.setImageResource(R.mipmap.guide_dot_normal);
            ivInd3.setImageResource(R.mipmap.guide_dot_normal);
            ivInd4.setImageResource(R.mipmap.guide_dot_normal);
        }
        if (i == 2) {
            //第二页
            ivInd1.setImageResource(R.mipmap.guide_dot_normal);
            ivInd2.setImageResource(R.mipmap.guide_dot_selected);
            ivInd3.setImageResource(R.mipmap.guide_dot_normal);
            ivInd4.setImageResource(R.mipmap.guide_dot_normal);
        }
        if (i == 3) {
            //第三页
            ivInd1.setImageResource(R.mipmap.guide_dot_normal);
            ivInd2.setImageResource(R.mipmap.guide_dot_normal);
            ivInd3.setImageResource(R.mipmap.guide_dot_selected);
            ivInd4.setImageResource(R.mipmap.guide_dot_normal);
        }
        if (i == 4) {
            //第四页
            ivInd1.setImageResource(R.mipmap.guide_dot_normal);
            ivInd2.setImageResource(R.mipmap.guide_dot_normal);
            ivInd3.setImageResource(R.mipmap.guide_dot_normal);
            ivInd4.setImageResource(R.mipmap.guide_dot_selected);
        }
    }

    /**
     * 初始化界面
     */
    private void initView() {
        vp = (ViewFlipper) findViewById(R.id.guide_flipper);

        ivInd1 = (ImageView) findViewById(R.id.iv_ind1);
        ivInd2 = (ImageView) findViewById(R.id.iv_ind2);
        ivInd3 = (ImageView) findViewById(R.id.iv_ind3);
        ivInd4 = (ImageView) findViewById(R.id.iv_ind4);

        btnEnter = (Button) findViewById(R.id.btn_enter);

        btnEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppContext.getInstance().setProperty("nofirst", "true");
                UIHelper.showActivityAndFinish(GuideActivity.this, LoginActivity.class);
            }
        });

        gesture = new GestureDetector(this, new GestureDetector.OnGestureListener() {
            // 用户（轻触触摸屏后）松开，由一个1个MotionEvent ACTION_UP触发
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
//                System.out.println("onSingleTapUp");
                return false;
            }

            // 用户轻触触摸屏，尚未松开或拖动，由一个1个MotionEvent ACTION_DOWN触发
            // 注意和onDown()的区别，强调的是没有松开或者拖动的状态
            @Override
            public void onShowPress(MotionEvent e) {
//                System.out.println("onShowPress");

            }

            // 用户按下触摸屏，并拖动，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                    float distanceX, float distanceY) {
//                System.out.println("onScroll");
                return false;
            }

            // 用户长按触摸屏，由多个MotionEvent ACTION_DOWN触发
            @Override
            public void onLongPress(MotionEvent e) {
//                System.out.println("onLongPress");

            }

            // 用户按下触摸屏、快速移动后松开，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE,
            // 1个ACTION_UP触发
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2,
                                   float velocityX, float velocityY) {
                if (e1.getX() - e2.getX() > 120) {// 向右滑动
                    vp.setInAnimation(AnimationUtils.loadAnimation(GuideActivity.this,
                            R.anim.in_rightleft));
                    vp.setOutAnimation(AnimationUtils.loadAnimation(GuideActivity.this,
                            R.anim.out_rightleft));
                    //当前是不是最后一个 可以进行滑
                    if (vp.getCurrentView().getId() != R.id.guide_4) {
                        vp.showNext();
                        selectIndex(++index);
                    }
                } else if (e2.getX() - e1.getX() > 120) {// 向左滑动
                    vp.setInAnimation(AnimationUtils.loadAnimation(GuideActivity.this,
                            R.anim.in_leftright));
                    vp.setOutAnimation(AnimationUtils.loadAnimation(GuideActivity.this,
                            R.anim.out_leftright));

                    if (vp.getCurrentView().getId() != R.id.guide_1) {
                        vp.showPrevious();
                        selectIndex(--index);
                    }

                }
                if (vp.getCurrentView().getId() == R.id.guide_4) {
                    btnEnter.setVisibility(View.VISIBLE);
                } else {
                    btnEnter.setVisibility(View.GONE);
                }
                return false;
            }

            // 用户轻触触摸屏，由1个MotionEvent ACTION_DOWN触发
            @Override
            public boolean onDown(MotionEvent e) {
//                System.out.println("onDown");
                return false;
            }
        });
        gesture.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
            // 短快的点击算一次单击
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
//                System.out.println("onSingleTapConfirmed");
                return false;
            }

            // 双击时产生一次
            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
//                System.out.println("onDoubleTapEvent");
                return false;
            }

            // 双击时产生两次
            @Override
            public boolean onDoubleTap(MotionEvent e) {
//                System.out.println("onDoubleTap");
                return false;
            }
        });

        selectIndex(index);
    }

    private int index = 1;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gesture.onTouchEvent(event);
    }

    private View getImageView(int id) {
        ImageView imgView = new ImageView(this);
        imgView.setId(id);
        imgView.setImageResource(id);
        return imgView;
    }

    public void onResume() {
        super.onResume();
        //友盟统计
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        //友盟统计
        MobclickAgent.onPause(this);
    }


}
